<?php
/************************************
 FILE           : yahooboss.class.php
 Description    : To perform Simple yahoo web search, image search and news search by using yahoo boss apis and return the resultset.  
 VERSION        : 1.0                                                  
 AUTHOR         : Hastimal C. Shah
 E-Mail         : hasti.shah@gmail.com
 last update    : 24 July 2008     
**************************************/
class YahooBoss
{
	var $mYahooBossAppId  = 'xwVCu0LV34FGDKM6ZvaI0g8Am4sc0E4BaP9I6MZZ.UAOZ_8wP5D4AYjgcYha_al0aOKTp400';// Give you application Id here complusory
	var $mYahooHost       = "http://boss.yahooapis.com";
	var $mFormat		  = array("xml"=>"xml","json"=>"json");
	var $mAttributes      = array("count"=>"count","start"=>"start","totalhits"=>"totalhits","deephits"=>"deephits");
    var $mError           = '';           


	public function YahooWebSearch($query,$format,$optionalParameters= '',$attributes = '')
	{
		/*  Parameters description are as under
			1-> $query contains the query or keywords to be searched.[REQUIRED]
			2-> $format contains the resultset format[XML,JSON]  [REQUIRED].
			3-> $optionalParameters for future enhancement.
			4-> $attributes for future enhancement.
		 */
		
		$searchDirectory	= "/ysearch/web/v1/";

		if('' == $query)
		{
			$this->mError = "Query is blank";
			return false;
		}

		$this->_CheckFormat($format);
			
		$url = $this->mYahooHost.$searchDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format;

		//Get the results

		$results = $this->_processCurl($url);

		return $results;
	}

	function _processCurl($url)
	{
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_PROXY, "http://192.168.4.102:4044");
                curl_setopt($ch, CURLOPT_PROXYPORT, 4044);
		$data = curl_exec($ch);
                curl_close($ch);
                $results = new SimpleXmlElement($data, LIBXML_NOCDATA);	
		return $results;
	}
	function YahooImagesSearch($query,$format,$optionalParameters= '',$attributes = '')
	{
		/*  Parameters description are as under
			1-> $query contains the query or keywords to be searched.[REQUIRED]
			2-> $format contains the resultset format[XML,JSON]  [REQUIRED].
			3-> $optionalParameters for future enhancement.
			4-> $attributes for future enhancement.
		 */
		
		$searchDirectory	= "/ysearch/images/v1/";

		if('' == $query)
		{
			$this->mError = "Query is blank";
			return false;
		}

		$this->_CheckFormat($format);
			
		$url = $this->mYahooHost.$searchDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format;
	
		//Get the results

		$results=$this->_processCurl($url);
		return $results;
	}

	function YahooNewsSearch($query,$format,$optionalParameters= '',$attributes = '')
	{
		/*  Parameters description are as under
			1-> $query contains the query or keywords to be searched.[REQUIRED]
			2-> $format contains the resultset format[XML,JSON]  [REQUIRED].
			3-> $optionalParameters for future enhancement.
			4-> $attributes for future enhancement.
		 */
		
		$searchDirectory	= "/ysearch/news/v1/";

		if('' == $query)
		{
			$this->mError = "Query is blank";
			return false;
		}

		$this->_CheckFormat($format);
			
		$url = $this->mYahooHost.$searchDirectory.$query.'?appid='.$this->mYahooBossAppId.'&format='.$format;
		//Get the results

		$results=$this->_processCurl($url);
		return $results;
	}


	function _CheckFormat($format)
	{
		if('' == $format)
		{
			$this->mError = "Please give the return resultset format, it is blank!";
			return false;
		}
		else
		{
			$checkFormat = array_key_exists($format,$this->mFormat);
			if(!$checkFormat)
			{
				$this->mError = "Please give the correct format, xml or json!";
				return false;
			}
			else
			{
				return true;
			}
		}
	
	}


	function GetError()
	{
		return $this->mError;
	}
}

?>